Setup and storing functions

knitr::opts_chunk$set(echo = TRUE, warning = FALSE, message = FALSE)
library(here)
library(tidyverse)
library(janitor)
library(broom)
library(equatiomatic)

options(scipen = 999)


electricity <- read_csv(here("HW3_data.csv")) %>% 
  clean_names() %>% 
  select(-1)

# running linear models to get demand curves for high and low income consumers
model_demand_l <- lm(price_cents  ~ q_low_kwh, data = electricity)
model_demand_h <- lm(price_cents ~ q_high_kwh, data = electricity)


## FUNCTIONS

# demand 
demand <- function(p, model){
  q <- (p - model$coefficients[[1]])/model$coefficients[[2]]
  q <- ifelse(q<0,0,q)
  return(q)
}

# aggregate demand
demand_agg <- function(p){
  q <- demand(p, model_demand_l) + demand(p, model_demand_h)
  return(q)
}

# consumer surplus
CS <- function(p, model){
  q <- demand(p, model)
  cs <- 0.5*(model$coefficients[[1]] - p)*q
  return(cs)
}

# aggregate consumer surplus
CS_agg <- function(p){
  cs <- CS(p,model_demand_l) + CS(p,model_demand_h)
  return(cs)
}

# producer surplus function
PS <- function(p){
  ps <- (p*demand_agg(p))/2 # producer surplus
  return(ps)
}

# marginal cost functions
mc_slope <- 10/demand_agg(10)
# marginal cost function
mc <- function(q){
  p <- q*(mc_slope)
  return(p)
}

mc_q <- function(p){
  q <- p/mc_slope
  return(q)
}

# social cost of carbon to mec function
scc <- function(scc){
  mec <- (0.85/metric_ton)*100*scc
  return(mec)
}
# determining marginal external cost
metric_ton <- 2204.62
mec_cents <- (0.85/metric_ton)*100*51

1. Price per kwh of electricity

metric_ton <- 2204.62
price_per_kwh_cents<- (0.85/metric_ton)*100*51

Marginal external cost per kWh of electricity: 1.97 cents


2. Supply and demand curves, consumer/producer benefits

# creating a table for demand based on the linear model
price_vector <- c(0:35)
table <- data.frame(price_vector)
demand_table <- table %>% 
  mutate(demand_low = demand(price_vector, model = model_demand_l)) %>% 
  mutate(demand_high = demand(price_vector, model = model_demand_h)) %>% 
  mutate(demand_agg = demand_agg(price_vector)) %>% 
  rename(price_cents = price_vector)

demand_table_longer <- demand_table %>% 
  pivot_longer(2:4, names_to = "demand_level", values_to = "demand")

# plotting supply and demand
ggplot(data = demand_table_longer, aes(x = demand, y = price_cents, color = demand_level)) +
  geom_line(size = 1) +
  theme_minimal(14) +
  scale_color_manual(values = c("darkblue", "cyan4", "cadetblue3", "black", "firebrick")) +
  scale_x_continuous(limits = c(1, 9e+05)) +
  theme(legend.position = "none") + 
  geom_line(data = demand_table_longer, aes(x = demand, y = mec_cents + mc_slope*demand),
            color = "goldenrod4",
            size = 1) +
  geom_line(data = demand_table_longer, aes(x = demand, y = mec_cents),
            color = "firebrick",
            size = 1) +
  labs(x = "kWh electricity used", y= "Price (cents)") +
  theme(legend.title = element_blank()) +
  geom_text(aes(label = "Low income demand", x = 140000, y = 11), angle = -61, size = 3, color = "cadetblue3") +
  geom_text(aes(label = "High income demand", x = 370000, y = 14), angle = -43, size = 3, color = "cyan4") +
  geom_text(aes(label = "Aggregate demand", x = 620000, y = 8.3), angle = -31, size = 3, color = "darkblue") +
  geom_text(aes(label = "Supply", x = 750000, y = 17), angle =15, size = 3, color = "goldenrod4") +
  geom_text(aes(label = "Environmental damage", x = 350000, y = 3), size = 3, color = "Firebrick")

# storing slopes and intercepts of each equation to report inline code 
int_high <- model_demand_h$coefficients[1]
slope_high <- model_demand_h$coefficients[2]
int_low <- model_demand_l$coefficients[1]
slope_low <- model_demand_l$coefficients[2]

# calculating surpluses and environmental costs
cs2 <- CS_agg(10) # consumer surplus
ps2 <- 10*demand_agg(10)/2 # producer surplus
enviro_cost_3 <- mec_cents*demand_agg(10) # environmental cost

Demand curve for high income consumers: P = 31.61 -0.000052Q

Demand curve for low income consumers: P = 23.37 -0.00011Q

Aggregate demand curve: To find the aggregate demand curve, horizontally sum the two demand curves above. FIND ANSWER TO THIS

Supply curve: P = 0.000019Q

Consumer benefit: $52987.22

Producer benefit: $26835.97

Environmental Cost: $10553.65


3. Consumer benefit by population

# finding the consumer surplus for each income group
cs_low3 <- CS(10, model_demand_l) # consum
cs_high3 <- CS(10, model = model_demand_h)

Consumer benefit for high income consumers: $44874.79

Consumer benefit for low income consumers: $8112.43


4. Optimal electricity tax

# finding the new demand curve under the tax 
demand_after_tax <- function(p, model, mec){
  q <- (p - model$coefficients[[1]] + mec)/model$coefficients[[2]]
  q <- ifelse(q<0,0,q)
  return(q)
}

# aggregate demand curve function after tax
demand_agg_after_tax <- function(p){
  q <- demand_after_tax(p, model_demand_l, mec = mec_cents) + demand_after_tax(p, model_demand_h, mec = mec_cents)
  return(q)
}
# setting the functions equal to determine new equilibrium price after tax
uniroot_after_tax <- uniroot(function(p)
  demand_agg_after_tax(p) - mc_q(p),
        interval = c(0,20))

# new consumer surplus function
CS_after_tax <- function(p, model){
  q <- demand_after_tax(p, model)
  cs <- 0.5*(model$coefficients[[1]] - p)*q
  return(cs)
}

price_after_tax<- uniroot_after_tax$root

# total environmental damage
enviro_damage_after_tax <- mec_cents*demand_agg_after_tax(price_after_tax)
  
tax_revenue <- mec_cents*demand_agg_after_tax(price_after_tax)

# amount of electricity consumed after tax
consumption_after_tax <- demand_agg_after_tax(price_after_tax)

# overall welfare to high and low income consumers
cs_high_after_tax <- CS(p = price_after_tax + mec_cents, model = model_demand_h)
cs_low_after_tax <- CS(p = price_after_tax + mec_cents, model = model_demand_l) - enviro_damage_after_tax

# welfare for electricity producer
PS_after_tax <- function(p){
  ps <- (p*demand_agg_after_tax(p))/2 # producer surplus
  return(ps)
}
ps_after_tax <- PS_after_tax(price_after_tax)

Optimal Electricity Tax: 1.97 cents

A. The amount of electricity produced and consumed: 500284.52kWh

B. The price of electricity: 9.32 cents per kWh before the tax is applied

C. Overall welfare of “high” income consumers: $39686.25

D. Overall welfare of “low” income consumers: $-3211.85, meaning there is a net loss of welfare to low income consumers

E. Wlefare to power suppliers: $23316.147

F. Total environmental damage: $9837.22

G. Total tax revenue generated: $9837.22


5. Tax redistribution

# calculating relative electricity use under the status quo 
proportion_high <- demand(model_demand_h, p = 10)/((demand(model_demand_h, p = 10)) + demand(model_demand_l, p = 10))
proportion_low <- demand(model_demand_l, p = 10)/((demand(model_demand_h, p = 10)) + demand(model_demand_l, p = 10))

welfare_h_5 <- (cs_high_after_tax + proportion_high*tax_revenue)/100 
welfare_l_5 <- (cs_low_after_tax + proportion_low*tax_revenue)/100 
ps_5 <- ps_after_tax/100

SCC = $51

Overall welfare of “high” income consumers: $47299.43

Overall welfare of “low” income consumers: $-987.81

Welfare to power suppliers: $23316.147


SCC = $75

scc5 <- 75

demand_after_tax <- function(p, model, mec){
  q <- (p - model$coefficients[[1]] + mec)/model$coefficients[[2]]
  q <- ifelse(q<0,0,q)
  return(q)
}

# aggregate demand curve function after tax
demand_agg_after_tax <- function(p, mec){
  q <- demand_after_tax(p, model_demand_l, mec = mec) + demand_after_tax(p, model_demand_h, mec = mec)
  return(q)
}

# new consumer surplus function
CS_after_tax <- function(p, model){
  q <- demand_after_tax(p, model)
  cs <- 0.5*(model$coefficients[[1]] - p)*q
  return(cs)
}

# setting the functions equal to determine new equilibrium price after tax
uniroot_after_tax <- uniroot(function(p)
  demand_agg_after_tax(p, mec = scc(scc5)) - mc_q(p),
        interval = c(0,20))

price_after_tax <- uniroot_after_tax$root

# total environmental damage
enviro_damage_after_tax <- scc(scc5)*demand_agg_after_tax(price_after_tax, mec = scc(scc5))
  
tax_revenue <- scc(scc5)*demand_agg_after_tax(price_after_tax, mec = scc(scc5))

# amount of electricity consumed after tax
consumption_after_tax <- demand_agg_after_tax(price_after_tax, mec = scc(scc5))

# overall welfare to high and low income consumers
cs_high_after_tax <- CS(p = price_after_tax + scc(scc5), model = model_demand_h)
cs_low_after_tax <- CS(p = price_after_tax + scc(scc5), model = model_demand_l) - enviro_damage_after_tax

# welfare for electricity producer
PS_after_tax <- function(p, mec){
  ps <- (p*demand_agg_after_tax(p, mec = mec))/2 # producer surplus
  return(ps)
}
ps_after_tax <- PS_after_tax(price_after_tax, mec = scc(scc5))

welfare_h_5 <- (cs_high_after_tax + proportion_high*tax_revenue)/100 
welfare_l_5 <- (cs_low_after_tax + proportion_low*tax_revenue)/100 
ps_5 <- ps_after_tax/100

Overall welfare of “high” income consumers: $48166.99

Overall welfare of “low” income consumers: $-4834.52

Welfare to power suppliers: $21745.342


SCC = $100

# inputting social cost of carbon
scc5 <- 100

# finding the new demand curve under the tax 
demand_after_tax <- function(p, model, mec){
  q <- (p - model$coefficients[[1]] + mec)/model$coefficients[[2]]
  q <- ifelse(q<0,0,q)
  return(q)
}

# aggregate demand curve function after tax
demand_agg_after_tax <- function(p, mec){
  q <- demand_after_tax(p, model_demand_l, mec = mec) + demand_after_tax(p, model_demand_h, mec = mec)
  return(q)
}

# new consumer surplus function
CS_after_tax <- function(p, model){
  q <- demand_after_tax(p, model)
  cs <- 0.5*(model$coefficients[[1]] - p)*q
  return(cs)
}

# setting the functions equal to determine new equilibrium price after tax
uniroot_after_tax <- uniroot(function(p)
  demand_agg_after_tax(p, mec = scc(scc5)) - mc_q(p),
        interval = c(0,20))

price_after_tax <- uniroot_after_tax$root

# total environmental damage
enviro_damage_after_tax <- scc(scc5)*demand_agg_after_tax(price_after_tax, mec = scc(scc5))
  
tax_revenue <- scc(scc5)*demand_agg_after_tax(price_after_tax, mec = scc(scc5))

# amount of electricity consumed after tax
consumption_after_tax <- demand_agg_after_tax(price_after_tax, mec = scc(scc5))

# overall welfare to high and low income consumers
cs_high_after_tax <- CS(p = price_after_tax + scc(scc5), model = model_demand_h)
cs_low_after_tax <- CS(p = price_after_tax + scc(scc5), model = model_demand_l) - enviro_damage_after_tax

# welfare for electricity producer
PS_after_tax <- function(p, mec){
  ps <- (p*demand_agg_after_tax(p, mec = mec))/2 # producer surplus
  return(ps)
}
ps_after_tax <- PS_after_tax(price_after_tax, mec = scc(scc5))

welfare_h_5 <- (cs_high_after_tax + proportion_high*tax_revenue)/100 
welfare_l_5 <- (cs_low_after_tax + proportion_low*tax_revenue)/100 
ps_5 <- ps_after_tax/100

Overall welfare of “high” income consumers: $48884.6

Overall welfare of “low” income consumers: $-8544.95

Welfare to power suppliers: $20167.331


SCC = $125

# inputting social cost of carbon
scc5 <- 125

# finding the new demand curve under the tax 
demand_after_tax <- function(p, model, mec){
  q <- (p - model$coefficients[[1]] + mec)/model$coefficients[[2]]
  q <- ifelse(q<0,0,q)
  return(q)
}

# aggregate demand curve function after tax
demand_agg_after_tax <- function(p, mec){
  q <- demand_after_tax(p, model_demand_l, mec = mec) + demand_after_tax(p, model_demand_h, mec = mec)
  return(q)
}

# new consumer surplus function
CS_after_tax <- function(p, model){
  q <- demand_after_tax(p, model)
  cs <- 0.5*(model$coefficients[[1]] - p)*q
  return(cs)
}

# setting the functions equal to determine new equilibrium price after tax
uniroot_after_tax <- uniroot(function(p)
  demand_agg_after_tax(p, mec = scc(scc5)) - mc_q(p),
        interval = c(0,20))

price_after_tax <- uniroot_after_tax$root

# total environmental damage
enviro_damage_after_tax <- scc(scc5)*demand_agg_after_tax(price_after_tax, mec = scc(scc5))
  
tax_revenue <- scc(scc5)*demand_agg_after_tax(price_after_tax, mec = scc(scc5))

# amount of electricity consumed after tax
consumption_after_tax <- demand_agg_after_tax(price_after_tax, mec = scc(scc5))

# overall welfare to high and low income consumers
cs_high_after_tax <- CS(p = price_after_tax + scc(scc5), model = model_demand_h)
cs_low_after_tax <- CS(p = price_after_tax + scc(scc5), model = model_demand_l) - enviro_damage_after_tax

# welfare for electricity producer
PS_after_tax <- function(p, mec){
  ps <- (p*demand_agg_after_tax(p, mec = mec))/2 # producer surplus
  return(ps)
}
ps_after_tax <- PS_after_tax(price_after_tax, mec = scc(scc5))

welfare_h_5 <- (cs_high_after_tax + proportion_high*tax_revenue)/100 
welfare_l_5 <- (cs_low_after_tax + proportion_low*tax_revenue)/100 
ps_5 <- ps_after_tax/100

Overall welfare of “high” income consumers: $49412.33

Overall welfare of “low” income consumers: $-11952.77

Welfare to power suppliers: $18648.753


SCC = $150

# inputting social cost of carbon
scc5 <- 150

# finding the new demand curve under the tax 
demand_after_tax <- function(p, model, mec){
  q <- (p - model$coefficients[[1]] + mec)/model$coefficients[[2]]
  q <- ifelse(q<0,0,q)
  return(q)
}

# aggregate demand curve function after tax
demand_agg_after_tax <- function(p, mec){
  q <- demand_after_tax(p, model_demand_l, mec = mec) + demand_after_tax(p, model_demand_h, mec = mec)
  return(q)
}

# new consumer surplus function
CS_after_tax <- function(p, model){
  q <- demand_after_tax(p, model)
  cs <- 0.5*(model$coefficients[[1]] - p)*q
  return(cs)
}

# setting the functions equal to determine new equilibrium price after tax
uniroot_after_tax <- uniroot(function(p)
  demand_agg_after_tax(p, mec = scc(scc5)) - mc_q(p),
        interval = c(0,20))

price_after_tax <- uniroot_after_tax$root

# total environmental damage
enviro_damage_after_tax <- scc(scc5)*demand_agg_after_tax(price_after_tax, mec = scc(scc5))
  
tax_revenue <- scc(scc5)*demand_agg_after_tax(price_after_tax, mec = scc(scc5))

# amount of electricity consumed after tax
consumption_after_tax <- demand_agg_after_tax(price_after_tax, mec = scc(scc5))

# overall welfare to high and low income consumers
cs_high_after_tax <- CS(p = price_after_tax + scc(scc5), model = model_demand_h)
cs_low_after_tax <- CS(p = price_after_tax + scc(scc5), model = model_demand_l) - enviro_damage_after_tax

# welfare for electricity producer
PS_after_tax <- function(p, mec){
  ps <- (p*demand_agg_after_tax(p, mec = mec))/2 # producer surplus
  return(ps)
}
ps_after_tax <- PS_after_tax(price_after_tax, mec = scc(scc5))

welfare_h_5 <- (cs_high_after_tax + proportion_high*tax_revenue)/100 
welfare_l_5 <- (cs_low_after_tax + proportion_low*tax_revenue)/100 
ps_5 <- ps_after_tax/100

Overall welfare of “high” income consumers: $49750.16

Overall welfare of “low” income consumers: $-15057.98

Welfare to power suppliers: $17189.609


6. Solar generation

# demand fucntion after tax
demand6 <- function(p, model){
  q <- (p - (0.5*model$coefficients[1]))/model$coefficients[2]
  q <- ifelse(q<0,0,q)
  return(q)
}

# aggregate demand curve function after tax
demand_agg6 <- function(p){
  q <- demand6(p, model_demand_l) + demand6(p, model_demand_h)
  return(q)
}
# setting the functions equal to determine new equilibrium price
uniroot_after_tax <- uniroot(function(p)
  demand_agg6(p) - mc_q(p),
        interval = c(0,20))

price_after_tax <- uniroot_after_tax$root

electricity6 <- demand_agg6(price_after_tax)

enviro_cost_6 <- electricity6*mec_cents


# need to cut environmental cost nearly in half, so we divide the tax by that amount
c6 <- (mec_cents/(enviro_cost_6/9478))*100

A. Total electricity consumption: 268359.73 kWh

B. Total environmental externality: $5276.83

C. Tax to lower environmental damage to level with solar panels: 3.53 cents